<?php namespace App\Services\Admin;

use App\Facades\Infra\Admin\ArticleManage;
use App\Facades\Utils\QiniuFileManager;

class ArticleService{

	public function getArticles(){
		return ArticleManage::getArticles()->paginate(10);
	}

	public function getArticle($data){
		$id = $data['id'];

		return ArticleManage::getArticle($id);
	}

	public function createArticle($data){

		$data['cover'] = $this->getPricleUrl($data['cover']);

		ArticleManage::createArticle($data);
	}

	public function updateArticle($data){
		$id = $data['id'];

		if(isset($data['cover']) && $data['cover'] != null && $data['cover'] != ''){
			$data['cover'] = $this->getPricleUrl($data['cover']);
		}
		else{
			unset($data['cover']); 
		}

		ArticleManage::updateArticle($id,$data);
	}

	public function deleteArticle($data){
		$id = $data['id'];

		ArticleManage::deleteArticle($id);
	}

	public function getPricleUrl($key){
		return QiniuFileManager::getFileUrl($key,'public');
	}
}